package com.example.meeting.service.impl;

import com.example.meeting.mapper.GetMeetByIdMapper;
import com.example.meeting.mapper.MeetingMapper;
import com.example.meeting.mapper.SubjectMapper;
import com.example.meeting.model.domain.Meeting;
import com.example.meeting.model.domain.MeetingQuery;
import com.example.meeting.service.IMeetingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class MeetingServiceImpl implements IMeetingService {
    @Autowired
    private MeetingMapper meetingMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private GetMeetByIdMapper getMeetByIdMapper;

    @Override
    public Meeting selectMeetingById(Long id) {
        Meeting m = null;
        try {
            m = meetingMapper.selectMeetingById(id);
            if (m != null) {
                m.setSubjectList(subjectMapper.selectSubjectListByMeetingId(id));
            }
        } catch (Exception e){
            log.error("查询会议接口报错！{}",e.getMessage());
        }
        return m;
    }

    @Override
    public Meeting selectMeetingBySubjectId(Long subjectId) {
        try {
            return meetingMapper.selectMeetingBySubjectId(subjectId);
        } catch (Exception e){
            log.error("查询会议接口报错！{}",e.getMessage());
        }
        return null;
    }

    @Override
    public PageInfo<Meeting> selectMeetingList(Integer page, Integer pageSize, Integer user_id, Meeting meeting) {
        try {
            PageHelper.startPage(page,pageSize);
            //获取用户的所有会议id
            Integer[] meetingId = getMeetByIdMapper.getMeetById(user_id);
            if(meetingId.length==0) return new PageInfo<>(Collections.emptyList());


            MeetingQuery query = new MeetingQuery();
            query.setMeetingIds(meetingId.length != 0 ?
                    new ArrayList<>(Arrays.asList(meetingId)) :
                    new ArrayList<>());
            query.setId(meeting.getId()!=null ?meeting.getId().intValue():null);
            query.setName(meeting.getName());
            query.setStartTime(meeting.getStartTime());
            query.setEndTime(meeting.getEndTime());
            query.setStatus(meeting.getStatus());


            //根据id获取会议并放入集合
            List<Meeting> meetingList = meetingMapper.selectMeetingList(query);

            return new PageInfo<>(meetingList);
        } catch (Exception e){
            log.error("查询会议列表接口报错！{}",e.getMessage());
        }
        return null;
    }

    @Override
    public Integer addMeeting(Meeting meeting, Integer user_id) {
        try {
            int res = meetingMapper.addMeeting(meeting);
            //如果新增成功，则将用户与会议关联
            getMeetByIdMapper.linkUserMeet(user_id,meeting.getId().intValue());
            return res;
        } catch (Exception e){
            log.error("新增会议接口报错！{}",e.getMessage());
        }
        return 0;
    }

    @Override
    public Integer updateMeeting(Meeting meeting) {
        try {
            return meetingMapper.updateMeeting(meeting);
        } catch (Exception e){
            log.error("更新会议接口报错！{}",e.getMessage());
        }
        return 0;
    }

    @Transactional
    @Override
    public Integer deleteMeeting(Long id) {
        try {
            Integer res=0;

            //获取会议的状态，如果为已结束则删除会议及其议题
            Integer status = meetingMapper.selectStatusById(id);
            if (status!=null && status==2) subjectMapper.deleteByMId(id);
            res=meetingMapper.deleteMeeting(id);

            //根据会议id删除表中所有会议id为会议id的数据
            getMeetByIdMapper.deleteMeetById(id.intValue());

            return res;
        } catch (Exception e){
            log.error("删除会议接口报错！{}",e.getMessage());
        }
        return 0;
    }
}
